<?php

/*

	MMetaverse - Open Metaverse Access Library

	Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

	http://www.maryjanesoft.com
	http://www.marlonj.com

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General
	Public License along with this library; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place, Suite 330,
	Boston, MA  02111-1307  USA

	$Id$

*/

/**
 * @package mjs.mmetaverse.manager
 */

require_once dirname(__FILE__) . '/../types/MUUID.class.php';

require_once 'MAssetDB.class.php';
require_once 'MAssetManager.class.php';
require_once 'MConfig.class.php';
require_once 'MDataBase.class.php';
require_once 'MInventoryDB.class.php';
require_once 'MInventoryManager.class.php';
require_once 'MRegionDB.class.php';
require_once 'MRegionManager.class.php';
require_once 'MUserDB.class.php';
require_once 'MUserManager.class.php';

/**
 * Class that represents a Open Metaverse Grid 
 */
class MMetaverse {

    /**
     * Library Configuration
     * 
     * @var MConfig
     */
    private $config;

    /**
     * Managers
     */
    private $managers = array();

    /**
     * Creates a representation of the grid, loading the default configiration
     * file.
     *
     * @param string $configFilename Path to the configuration file
     */
    public function MMetaverse($configFilename = 'mmetaverse_conf.xml') {
        $this->config = MConfig::load($configFilename);
    }

    /**
     * Return the currento Configuration
     * 
     * @return MConfig 
     */
    public function getConfig() {
        return $this->config;
    }

    /**
     * Check user names and password to verify the identity 
     *
     * @param string $firstName First name of the user
     * @param string $lastName Last name of the user
     * @param string $password Password of the user 
     * @return boolean true if the user is valid, false otherwise 
     */
    public function authenticate($firstName, $lastName, $password) {
        // Get the user manager 
        $userManager = $this->getUserManager();
        // Authenticate 
        return $userManager->authenticate($firstName, $lastName, $password);
    }

    /**
     * Return the database
     *
     * @param string Database type user, asset, region 
     * @return MDataBase Database
     */
    public function getDataBase($type) {
        
        // Database 
        $db = NULL;
        
        // Select database
        switch($type)
        {
            case 'user' : $db = new MUserDB($this); break;
            case 'asset' : $db = new MAssetDB($this); break;
            case 'region' : $db = new MRegionDB($this); break;
            case 'inventory' : $db = new MInventoryDB($this); break;
        }

        // Return database 
        return $db;
    }

    /**
     * Return the User Manager
     *
     * @return MUserManager The user manager
     */
    public function getUserManager() {
        // Check if the user manager is already created, if not create it
        if(!isset($this->managers['user']))
            $this->managers['user'] = new MUserManager($this);

        // Return the manager
        return $this->managers['user'];
    }

    /**
     * Return the Region Manager
     *
     * @return MRegionManager The region manager
     */
    public function getRegionManager() {
        // Check if the user manager is already created, if not create it
        if(!isset($this->managers['region']))
            $this->managers['region'] = new MRegionManager($this);

        // Return the manager
        return $this->managers['region'];
    }

    /**
     * Return the Asset Manager
     *
     * @return MAssetManager The asset manager
     */
    public function getAssetManager() {
        // Check if the user manager is already created, if not create it
        if(!isset($this->managers['asset']))
            $this->managers['asset'] = new MAssetManager($this);

        // Return the manager
        return $this->managers['asset'];
    }

    /**
     * Return the Inventory Manager
     *
     * @return MInventoryManager The inventory manager
     */
    public function getInventoryManager() {
        // Check if the user manager is already created, if not create it
        if(!isset($this->managers['inventory']))
            $this->managers['inventory'] = new MInventoryManager($this);

        // Return the manager
        return $this->managers['inventory'];
    }
}

?>